package com.hj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hj.mapper.BookInfoMapper;
import com.hj.pojo.Bookinfo;
import com.hj.pojo.HttpResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class BookInfoController {
    // 依赖注入
    @Resource
    BookInfoMapper bookInfoMapper;

    @GetMapping("/all")
    public HttpResult all(){
        HttpResult httpResult = new HttpResult();
        httpResult.setCode(200);
        httpResult.setMessage("查询成功");
        httpResult.setData(bookInfoMapper.selectList(null));
        return httpResult ;
    }

    @PostMapping("/add")
    public String add(@RequestBody Bookinfo bk){
        bookInfoMapper.insert(bk);
        return "添加图书OK";
    }

    @PutMapping("/update")
    public HttpResult update(@RequestBody Bookinfo bookinfo){
        HttpResult httpResult = new HttpResult();

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("bid", bookinfo.getBid());

        bookInfoMapper.update(bookinfo, updateWrapper);
        httpResult.setCode(200);
        httpResult.setMessage("修改图书成功");
        return httpResult;
    }

    @PutMapping("/updateId")
    public HttpResult updateId(@RequestBody Bookinfo bookinfo){
        HttpResult httpResult = new HttpResult();
        bookInfoMapper.updateById(bookinfo);
        httpResult.setCode(200);
        httpResult.setMessage("修改图书成功");
        return httpResult;
    }


    @DeleteMapping("/delete/{bid}")
    public String delete(@PathVariable Integer bid){
        QueryWrapper<Bookinfo> wrapper = new QueryWrapper<>();
        wrapper. eq("bid",bid); //删除条件，书号为指定的值
        bookInfoMapper.delete(wrapper);

        return "删除图书OK";
    }
}
